/**
 * @filename:DepartmentServiceImpl Dec 11, 2023
 * @project Wonder Vue Admin Api  1.1.0
 * Copyright(c) 2022 Wonder Co. Ltd. 
 * All right reserved. 
 */
package com.wonder.app.service.impl;

import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.vtuzx.core.bean.dto.VtuzxMap;
import com.vtuzx.core.utils.VtuzxUtil;
import com.wonder.admin.service.IVtuzxKingdeeService;
import com.wonder.app.domain.Department;
import com.wonder.app.domain.Synbd;
import com.wonder.app.mapper.DepartmentDao;
import com.wonder.app.mapper.SynbdDao;
import com.wonder.app.service.DepartmentService;
import com.wonder.app.request.DepartmentRequest;
import com.wonder.app.service.SynbdService;
import com.wonder.bean.kingdee.VtuzxKingdeeCloudClient;
import com.wonder.bean.kingdee.xqc.ConvertDate;
import com.wonder.common.utils.StringUtils;
import com.wonder.common.utils.MessageUtils;
import com.wonder.common.common.AjaxResult;

import java.util.*;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

/**   
 * @Description:TODO(车间服务实现)
 *
 * @version: 1.1.0
 * @author: Wonder
 * 
 */
@Service
public class DepartmentServiceImpl  extends ServiceImpl<DepartmentDao, Department> implements DepartmentService  {

    /**
     * 验证车间信息
     * 
     * @param department
     * @param type
     * @return String
     */
    @Override
    public String validate(Department department,String type){
    
    
       return "";
    }
    
    
    /**
	 * Description:车间分页返回
	 * @param request //检索条件
	 * @param page //分页参数
	 * @return IPage<Department>   //分页记录
	 */
	@Override 
	public IPage<Department> selectList(DepartmentRequest request, Page<Department> page){
	    LambdaQueryWrapper<Department> lambdaQueryWrapper = new LambdaQueryWrapper<>();
	    
	    if (null != request.getFname()) {
	       lambdaQueryWrapper.like(Department::getFNAME, request.getFname());
	      }

		if (null != request.getFnumber()) {
			lambdaQueryWrapper.like(Department::getFNUMBER, request.getFnumber());
		}
		lambdaQueryWrapper.orderByDesc(Department::getId);	
		Page<Department> selectPage = this.baseMapper.selectPage(page, lambdaQueryWrapper);
		return selectPage;
    }

	@Autowired
	private DepartmentDao departmentDao;
	@Autowired
	private  DepartmentService departmentService;
	@Autowired
	private IVtuzxKingdeeService kingdeeService;
	@Autowired
	private ConvertDate convertDate;
	@Autowired
	private SynbdDao synbdDao;
	@Autowired
	private SynbdService synbdService;
	public void syncDepartment(String endTime) throws Exception {
			syncDepartment(0,50,endTime);
	}
        
    private void syncDepartment(int start,int limit,String endTime) throws Exception{

		String	cond = String.format("FCreateDate > '%s' or FModifyDate > '%s'", endTime, endTime);

		VtuzxKingdeeCloudClient clent = kingdeeService.loginKingdee();
		ArrayList<String> arrayList = new ArrayList<>();
		arrayList.add("FDEPTID");//车间内码
		arrayList.add("FCREATEORGID");//创建组织编码
		arrayList.add("FCREATEORGID.FName");//创建组织名称
		arrayList.add("Fnumber");//部门编码
		arrayList.add("FNAME");//部门名称
		arrayList.add("FCREATEDATE");//创建日期
		arrayList.add("FMODIFYDATE");//修改日期
		List<VtuzxMap> departmentQuery = kingdeeService.query(clent, "BD_DEPARTMENT", arrayList,
				cond, "FDEPTID", start, limit);
		List<Department> departmentInsert = new ArrayList<>();//新增
		List<Department> departmentUpdate = new ArrayList<>();//修改


		for (VtuzxMap department : departmentQuery) {
			Department department1 = new Department();
			String FDEPTID = department.getString("FDEPTID");//车间内码
			department1.setFDEPTID(department.getLong("FDEPTID"));//车间内码
			department1.setFCREATEORGNO(department.getLong("FCREATEORGID"));//创建组织编码
//			String FCREATEORGNO = department.getString("FCREATEORGID");//创建组织编码
			department1.setFCREATEORGNAME(department.getString("FCREATEORGID.FName"));//创建组织名称
			department1.setFNUMBER(department.getString("Fnumber"));
			department1.setFNAME(department.getString("FNAME"));
			String FCREATEDATE = department.getObject("FCREATEDATE");
			Date ConverFCREATEDATE = ConvertDate.convertToDatabaseDateTime(FCREATEDATE, "yyyy-MM-dd'T'HH:mm:ss.SSS");
			department1.setFCREATEDATE(ConverFCREATEDATE);
			String FMODIFYDATE = department.getObject("FMODIFYDATE");
			Date ConverFMODIFYDATE = ConvertDate.convertToDatabaseDateTime(FMODIFYDATE,"yyyy-MM-dd'T'HH:mm:ss.SSS");
			department1.setFMODIFYDATE(ConverFMODIFYDATE);
			String Convertime = VtuzxUtil.formatYmdHmsF(ConverFMODIFYDATE);
			QueryWrapper<Department> queryWrapper = new QueryWrapper<>();
			queryWrapper.eq("FDEPTID", FDEPTID);
			List<Department> DepartmentList = departmentDao.selectList(queryWrapper);//判断数据库是否存在仓库
			Long id = null;
			for (Department department2 :DepartmentList)
			{
				id = department2.getId();
			}
			if (id != null) {
				department1.setId(id);
			}
			if(DepartmentList.size() == 0){
				departmentInsert.add(department1);
			}
			//判断修改日期是否大于最后同步时间
			int result = 0;
			if (endTime != null) {
				result = Convertime.compareTo(endTime);
			}
			//需要修改 已经存在并且修改日期大于最后同步日期
			if(DepartmentList.size() > 0 && result >0)
			{
				departmentUpdate.add(department1);
			}
		}
		//新增仓库
		if(!departmentInsert.isEmpty()){
			departmentService.saveBatch(departmentInsert);
		}
		//修改仓库
		if(!departmentUpdate.isEmpty()){
			departmentService.updateBatchById(departmentUpdate);
//			departmentService.saveOrUpdate(departmentUpdate);
		}
		if (departmentQuery.size() >= limit) {
			syncDepartment(start + limit, limit,endTime);
		}
	}

}